package xyz.keyon.servlet;

import xyz.keyon.bean.Page;
import xyz.keyon.bean.Student;
import xyz.keyon.service.IStudentService;
import xyz.keyon.service.impl.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "QueryAllStudentsByPageServlet", urlPatterns = "/QueryAllStudentsByPageServlet")
public class QueryAllStudentsByPageServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        IStudentService studentService = new StudentServiceImpl();
        //分页类
        Page cPage = new Page();

        //数据总数
        int count = studentService.getTotalCount();
        cPage.setTotalCount(count);
        System.out.println("数据总数：" + count);

        //当前页的页码
        String tmpPage = request.getParameter("currentPage");
        int currentPage = 1;
        if(tmpPage != null){
            currentPage = Integer.parseInt(tmpPage);
        }
        cPage.setCurrentPage(currentPage);
        System.out.println("当前页码：" + currentPage);

        //页面大小
        int pageSize = 3;
        if(request.getParameter("pSize") != null) {
            pageSize = Integer.parseInt(request.getParameter("pSize"));
        }
        cPage.setPageSize(pageSize);
        System.out.println("页面大小：" + pageSize);

        //当前页的数据集
        List<Student> students = studentService.queryStudentsByPage(currentPage,pageSize);
        cPage.setStudents(students);
        System.out.println(students);

        request.setAttribute("cPage",cPage);
        request.getRequestDispatcher("pages/home.jsp").forward(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
